require 'View/UIBase/UIBase'

UIArenaMainEnter = UIBase.New(PanelResNames.UIArenaMainEnter, UIBase)
local _M = UIArenaMainEnter
_M._rszdPlayTime = 1000 -- 燃魂之巅入场动画持续时间(毫秒)

function _M.Init(isShowEffIn)
    _M.isShowEffIn = isShowEffIn
    UIBase.Init(_M)
end

function _M:OnCreate()
    if self.haveInit == nil then
        self.haveInit = true
 
        self.Graph_UI_ruchang_rszd = self:GetChild("Graph_UI_ruchang_rszd_1")
        self.enter_list = self:GetChild("EnterList")
        self.enter_list.itemRenderer = self.renderEnterListItem
 
        self:RegisterOnClick(self:GetChild("back"), self.ClickBack)  
        self:RegisterEvent(Config.EventType.SeasonTime, self.UpdateListData) 
        
    end
    _M:UpdateListData()
end

-- 更新item
function _M.renderEnterListItem( index, obj )
    -- print("renderEnterListItem-->", index, obj)  
    local type_controller = obj:GetController("type")
    local time_controller = obj:GetController("time")
    local icon_title = obj:GetChild("Titleicon")
    local remain_time = obj:GetChild("RemainTime")
    local txt_title = obj:GetChild("title53")
    local txt_title2 = obj:GetChild("title54")
    local icon_bg = obj:GetChild("Entryicon")

    time_controller:SetSelectedIndex(2);
    if index == 0 then
        _M.RefreshEnterItem1(  type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg )  
        UIRedDots.addViewListen(PanelResNames.UIArenaMainEnter, "UIArenaMainEnter_arena", obj, Config.RedDotsType.ArenaBattle) 
    elseif index == 1 then
    --     _M.RefreshEnterItem2(  type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg ) 
    --     UIRedDots.addViewListen(PanelResNames.UIArenaMainEnter, "UIArenaMainEnter_crossarena", obj, Config.RedDotsType.CrossArenaBattle)
    -- elseif index == 2 then
        _M.RefreshEnterItem3(  type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg  ) 
        UIRedDots.addViewListen(PanelResNames.UIArenaMainEnter, "UIArenaMainEnter_crossteam", obj, Config.RedDotsType.CrossTeamArenaBattle)
    elseif index == 2 then
        type_controller:SetSelectedIndex(1)
        _M.RefreshEnterItem4(  type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg  )
        --UIRedDots.addViewListen(PanelResNames.UIArenaMainEnter, "UIArenaMainEnter_fward", obj, Config.RedDotsType.FriendBattle)
    end
    obj.onClick:Clear()
    obj.onClick:Add(function()
        _M.OnClickEnterItem( index, obj )
    end)
end

function _M.RefreshEnterItem1( type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg )
    icon_title.url = "ui://UIArenaMainEnter/jjc_rk_title_jjc"
    icon_bg.url = "ui://UIArenaMainEnter/jjc_rk_jjc_bg"
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Arena, false) then
        return;
    end
    local arenaBaseInfo = logicMgr.ArenaManager.getBaseInfo()
    local count_down_time = 0
    if arenaBaseInfo ~= nil then
        local is_battle_time = nil
        if arenaBaseInfo.seasonEndTime > arenaBaseInfo.endTime then -- 准备阶段
            count_down_time = arenaBaseInfo.seasonEndTime 
            is_battle_time = false
        else -- 战斗阶段
            count_down_time = arenaBaseInfo.endTime
            is_battle_time = true
        end
        if count_down_time - Config.serverTime > 0 then
            local countDown = function() 
                local timeNum = (count_down_time - Config.serverTime) / 1000 
                if not is_battle_time and timeNum <= 0 then
                    if time_controller ~= nil then
                        time_controller:SetSelectedIndex(1)
                    end
                    if txt_title ~= nil then
                        txt_title.text = "赛季未开始"
                    end
                    _M.RemoveTimerCallId1()
                    arenaHandler.sendReqArenaInfo()
                    return
                end
                -- print("RefreshEnterItem1 ", count_down_time, Config.serverTime)
                local timeStr = timeToTimeLeft(Mathf.Floor(timeNum), true);
                if Mathf.Floor(timeNum / (3600 * 24)) > 0 then
                    timeStr = timeToTimeLeft(Mathf.Floor(timeNum), false, false, true);
                else
                    timeStr = timeToTimeFormat(Mathf.Floor(timeNum));
                end  
                if is_battle_time then
                    time_controller:SetSelectedIndex(0)
                    remain_time.text = timeStr
                else
                    time_controller:SetSelectedIndex(1)
                    remain_time.text = string.format(getLanguage("ArenaTips7"), timeStr)
                end
            end
            _M.RemoveTimerCallId1()
            countDown()
            _M.timer_call_id1 = utimer.timeCall(countDown, 1000, -1) 
        else
            _M.RemoveTimerCallId1()
            if time_controller ~= nil then
                time_controller:SetSelectedIndex(1)
            end
            if txt_title ~= nil then
                txt_title.text = "赛季未开始"
            end
        end
    end
end

function _M.RemoveTimerCallId1() 
    if _M.timer_call_id1 ~= nil then
        utimer.remove(_M.timer_call_id1)
        _M.timer_call_id1 = nil
    end 
end

-- function _M.RefreshEnterItem2( type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg )
--     icon_title.url = "ui://UIArenaMainEnter/jjwf_rk_title_kf"  
--     icon_bg.url = "ui://UIArenaMainEnter/jjc_rk_qt_bg"
--     local arenaCrossBaseInfo = logicMgr.ArenaManager.getCrossBaseInfo()
--     local count_cross_downtime = 0
--     if arenaCrossBaseInfo ~= nil then
--         local is_cross_battle_time = nil
--         if arenaCrossBaseInfo.seasonEndTime > arenaCrossBaseInfo.endTime then -- 准备阶段
--             count_cross_downtime = arenaCrossBaseInfo.seasonEndTime
--             is_cross_battle_time = false
--         else -- 战斗阶段
--             count_cross_downtime = arenaCrossBaseInfo.endTime
--             is_cross_battle_time = true
--         end
--         if count_cross_downtime - Config.serverTime > 0 then
--             local count_cross_down = function()
--                 local timeNum = (count_cross_downtime - Config.serverTime) / 1000
--                 if not is_cross_battle_time and timeNum <= 0 then
--                     _M.RemoveTimerCallId2()
--                     crossArenaHandler.sendReqCrossArenaInfo()
--                     return
--                 end
--                 -- print("RefreshEnterItem2 ", count_cross_downtime, Config.serverTime)
--                 local timeStr = timeToTimeLeft(Mathf.Floor(timeNum), true)
--                 if Mathf.Floor(timeNum / (3600 * 24)) > 0 then
--                     timeStr = timeToTimeLeft(Mathf.Floor(timeNum), false, false, true)
--                 else
--                     timeStr = timeToTimeFormat(Mathf.Floor(timeNum))
--                 end 
--                 if is_cross_battle_time then
--                     if time_controller ~= nil then
--                         time_controller:SetSelectedIndex(0)
--                     end
--                     if remain_time ~= nil then
--                         remain_time.text = timeStr
--                     end
--                 else
--                     if time_controller ~= nil then
--                         time_controller:SetSelectedIndex(1)
--                     end
--                     if txt_title ~= nil then
--                         txt_title.text = string.format(getLanguage("ArenaTips7"), timeStr)
--                     end
--                 end
--             end
--             _M.RemoveTimerCallId2()
--             _M.timer_call_id2 = utimer.timeCall(count_cross_down, 1000, -1)
--         else
--             _M.RemoveTimerCallId2()
--             if time_controller ~= nil then
--                 time_controller:SetSelectedIndex(1)
--             end
--             if txt_title ~= nil then
--                 txt_title.text = "赛季未开始"
--             end
--         end
--     end 
-- end

-- function _M.RemoveTimerCallId2() 
--     if _M.timer_call_id2 ~= nil then
--         utimer.remove(_M.timer_call_id2)
--         _M.timer_call_id2 = nil
--     end 
-- end

function _M.RefreshEnterItem3( type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg )
    icon_title.url = "ui://UIArenaMainEnter/jjjjc_rk_title"  
    icon_bg.url = "ui://UIArenaMainEnter/jjwf_rk03_bg"
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.CrossTeamArena, false) then
        return;
    end
    local arenaCrossTeamBaseInfo = logicMgr.ArenaManager.getCrossTeamBaseInfo()
    local count_cross_team_downtime = 0
    _M.RemoveTimerCallId3()
    if arenaCrossTeamBaseInfo ~= nil then
        local is_cross_team_battle_time = nil
        if arenaCrossTeamBaseInfo.seasonEndTime > arenaCrossTeamBaseInfo.endTime then -- 准备阶段
            count_cross_team_downtime = arenaCrossTeamBaseInfo.seasonEndTime
            is_cross_team_battle_time = false
        else -- 战斗阶段
            count_cross_team_downtime = arenaCrossTeamBaseInfo.endTime
            is_cross_team_battle_time = true
        end
        if count_cross_team_downtime - Config.serverTime > 0 then
            local count_cross_team_down = function()
                local timeNum = (count_cross_team_downtime - Config.serverTime) / 1000
                if not is_cross_team_battle_time and timeNum <= 0 then
                    _M.RemoveTimerCallId3()
                    crossTeamArenaHandler.sendReqHighArenaInfo()
                    return
                end
                if timeNum <= 0 then
                    _M.RemoveTimerCallId3()
                    crossTeamArenaHandler.sendReqHighArenaInfo()
                    if time_controller ~= nil then
                        time_controller:SetSelectedIndex(1)
                    end
                    if txt_title ~= nil then
                        txt_title.text = "赛季未开始"
                    end
                    return
                end
                -- print("RefreshEnterItem3 ", count_cross_team_downtime, Config.serverTime)
                local timeStr = timeToTimeLeft(Mathf.Floor(timeNum), true)
                if Mathf.Floor(timeNum / (3600 * 24)) > 0 then
                    timeStr = timeToTimeLeft(Mathf.Floor(timeNum), false, false, true)
                else
                    timeStr = timeToTimeFormat(Mathf.Floor(timeNum))
                end 
                if is_cross_team_battle_time then
                    if time_controller ~= nil then
                        time_controller:SetSelectedIndex(0)
                    end
                    if remain_time ~= nil then
                        remain_time.text = timeStr
                    end
                else
                    if time_controller ~= nil then
                        time_controller:SetSelectedIndex(1)
                    end
                    if txt_title ~= nil then
                        txt_title.text = string.format(getLanguage("ArenaTips7"), timeStr)
                    end
                end
            end
            _M.RemoveTimerCallId3()
            _M.timer_call_id3 = utimer.timeCall(count_cross_team_down, 1000, -1)
        else
            _M.RemoveTimerCallId3()
            if time_controller ~= nil then
                time_controller:SetSelectedIndex(1)
            end
            if txt_title ~= nil then
                txt_title.text = "赛季未开始"
            end
            _M.RemoveTimerCallId3()
            crossTeamArenaHandler.sendReqHighArenaInfo()
        end
    end 
end

function _M.RemoveTimerCallId3() 
    if _M.timer_call_id3 ~= nil then
        utimer.remove(_M.timer_call_id3)
        _M.timer_call_id3 = nil
    end 
end

function _M.RefreshEnterItem4( type_controller, time_controller, icon_title, remain_time, txt_title, txt_title2, icon_bg )
    type_controller:SetSelectedIndex(1)
    --[[icon_title.url = "ui://UIArenaMainEnter/jjc_rk_title_qyzc"
    icon_bg.url = "ui://UIArenaMainEnter/jjc_rk_hyz_bg"
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.FriendBattle, false) then
        return;
    end
    if logicMgr.FriendBattleManager.GetOpen() then
        time_controller:SetSelectedIndex(0)
        local friendBattleTime = logicMgr.FriendBattleManager.SeasonOverTime()
        if friendBattleTime then
            if math.floor(friendBattleTime / (3600 * 24)) > 0 then
                remain_time.text = timeToTimeLeft(Mathf.Floor(friendBattleTime), false, false, true);
            else
                remain_time.text = timeToTimeLeft(Mathf.Floor(3600 * 24),false,false,true)
            end
        else
            remain_time.text = ""
        end
    else
        time_controller:SetSelectedIndex(1)
        txt_title.text = "赛季未开始"
    end--]]
end

 
function _M.OnClickEnterItem( index, obj )
    if index == 0 then
        _M.onClickUIArenaMain( obj )
    elseif index == 1 then
    --     _M.onClickUICrossArenaMain( obj )
    -- elseif index == 2 then
        _M.onClickUICrossTeamArenaMain( obj)
    elseif index == 2 then
       -- _M.OpenFriendWar( obj )
    end
end

function _M.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Arena, _M.UpdateListData)
    ListenEvent(Config.EventType.Fresh_Cross_Arena, _M.UpdateListData)
    ListenEvent(Config.EventType.Fresh_Cross_Team_Arena, _M.UpdateListData)
end

function _M.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Arena, _M.UpdateListData);
    CloseEvent(Config.EventType.Fresh_Cross_Arena, _M.UpdateListData)
    CloseEvent(Config.EventType.Fresh_Cross_Team_Arena, _M.UpdateListData)
end

function _M:UpdateListData()
    if _M.enter_list ~= nil then
        UIRedDots.removeViewEvent(PanelResNames.UIArenaMainEnter)
        _M.enter_list.numItems = 3
    end
end

function _M:OnEnable()
    if _M.isShowEffIn then
        UIHeadMenu.isNeedShowMask(true);
        resMgr:LoadModelPrefab("UIEff/UI_ruchang_rszd", function(prefab)
            if self.effDrawWrapper1 == nil then
                self.effDrawWrapper1 = GoWrapper.New();
            end
            if self.effFire1Obj ~= nil then
                destroyImmediate(self.effFire1Obj);
                self.effFire1Obj = nil
            end
            self.effFire1Obj = newObject(prefab)
            self.effDrawWrapper1:SetWrapTarget(self.effFire1Obj,false)
            Util.Lua_SetLocalRotationXYZ(self.effFire1Obj.transform, 0, 0, 0);
            self.Graph_UI_ruchang_rszd:SetNativeObject(self.effDrawWrapper1)
        end)
        utimer.timeCall(function ()
            UIHeadMenu.isNeedShowMask();
            if self.effFire1Obj ~= nil then
                destroyImmediate(self.effFire1Obj);
                self.effFire1Obj = nil
            end
        end, self._rszdPlayTime, 1);
    end

    AudioManager.SetSwitch("battle_steps", "arr")
    AudioManager.SetSwitch("Battle_SwitchGroup", "arena")

    --FriendBattleHandler.ReqTimeInfo()
    logicMgr.FriendBattleManager.FirstOpenTime()
    -- self.enter_list.numItems = 4
    self.needShowHeadMenu = nil

    --if isOpenUI(PanelNames.UIHeadMenu) then
    --    self.needShowHeadMenu = true
    --    closeUI(PanelNames.UIHeadMenu)
    --end
    self.LocalListenEvent()
end

function _M:OnDisable()
    _M.RemoveTimerCallId1()
    -- _M.RemoveTimerCallId2()  
    _M.RemoveTimerCallId3()
    UIRedDots.removeViewEvent(PanelResNames.UIArenaMainEnter) 
    _M.LocalCloseEvent()
end

function _M:OnDestroy()
    UIRedDots.removeViewEvent(PanelResNames.UIArenaMainEnter)
    _M.LocalCloseEvent()
    _M.RemoveTimerCallId1()
    -- _M.RemoveTimerCallId2()  
    _M.RemoveTimerCallId3()
    _M.haveInit = nil
    _M.Graph_UI_ruchang_rszd = nil
end

----------------------------onClick-----------------------------------
function _M:onClickUIArenaMain( obj )
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Arena, true, obj) then
        local arenaBaseInfo = logicMgr.ArenaManager.getBaseInfo();
        if (arenaBaseInfo.endTime - Config.serverTime) > 0 then
            showUI(PanelNames.UIArenaMain, EnumConst.ArenaBattleType.Normal) 
        else
            UISysTips.AddMsg(getLanguage("ArenaTips6"))
        end
    end
end

function _M:OpenFriendWar( obj )
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.FriendBattle, true, obj) then
        if logicMgr.FriendBattleManager.GetOpen() then
            local info = logicMgr.FriendBattleManager.GetSeasonTimeInfo()
            if info.apply == 1 then
                SetPlayerPrefsAfter5AMFlag("friendBattleEveryDay")
                FireEvent(Config.EventType.friendBattleEveryDay, true)
                showUI(PanelNames.UIFriendWar)
            else
                if logicMgr.FriendBattleManager.IsNext() then
                    showUI(PanelNames.UIFriendWar)
                else
                    UISysTips.AddMsg(getLanguage("FriendwarTips8"))
                end
            end
        else
            UISysTips.AddMsg("赛季未开始")
            return
        end
    end
end

-- 跨服竞技场
function _M:onClickUICrossArenaMain( obj )
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.CrossArena, true, obj) then
        local arenaCrossBaseInfo = logicMgr.ArenaManager.getCrossBaseInfo();
        if (arenaCrossBaseInfo.endTime - Config.serverTime) > 0 then
            showUI(PanelNames.UIArenaMain, EnumConst.ArenaBattleType.Cross)
        else
            UISysTips.AddMsg(getLanguage("ArenaTips6"))
        end
    end
end

-- 跨服组队竞技场
function _M:onClickUICrossTeamArenaMain( obj )
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.CrossTeamArena, true, obj) then
        local arenaCrossTeamBaseInfo = logicMgr.ArenaManager.getCrossTeamBaseInfo();
        if (arenaCrossTeamBaseInfo.endTime - Config.serverTime) > 0 then
            showUI(PanelNames.UIArenaMain, EnumConst.ArenaBattleType.TeamCross)
            logicMgr.ArenaManager.updateCrossTeamOpenStatus();
        else
            UISysTips.AddMsg(getLanguage("ArenaTips6"))
        end
    end
end

function _M:DefenTeam()

end

function _M:ClickBack()
    closeUI(PanelNames.UIArenaMainEnter)

    --showUI(PanelNames.UIHeadMenu)
    AudioManager.RefreshBGM()
end
----------------------------onClick-----------------------------------

return _M




